package com.middlegroup.pay.config;

import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer;
import com.middlegroup.common.utils.RedisObjectSerializer;
import org.apache.shiro.session.Session;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.io.Serializable;
import java.time.Duration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * TODO 修改database
 *
 * @author pc
 */
@Configuration
@EnableCaching
public class RedisCacheAutoConfiguration {

    @Bean("redisSessionTemplate")
    public RedisTemplate<Serializable, Session> redisSessionTemplate(LettuceConnectionFactory redisConnectionFactory) {
        RedisTemplate<Serializable, Session> template = new RedisTemplate<>();
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new RedisObjectSerializer());
        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }

    @Bean("redisCacheTemplate")
    public RedisTemplate<String, Serializable> redisCacheTemplate(LettuceConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, Serializable> template = new RedisTemplate<>();
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }

    @Bean
    public CacheManager cacheManager(RedisConnectionFactory factory) {
//        RedisCacheConfiguration cacheConfiguration = RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofDays(1))//缓存1天
//                .disableCachingNullValues().computePrefixWith(cacheName -> "middle_pay".concat(":").concat(cacheName).concat(":"))
//                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new
//                        GenericJackson2JsonRedisSerializer()));
        RedisCacheConfiguration cacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
                .disableCachingNullValues().computePrefixWith(cacheName -> "middle_pay".concat(":").concat(cacheName).concat(":"))
                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new
                        GenericJackson2JsonRedisSerializer()));
        // 设置一个初始化的缓存空间set集合
        Set<String> cacheNames = new HashSet<>();
        cacheNames.add("router");
        cacheNames.add("pay_platform");

        // 对每个缓存空间应用不同的配置
        Map<String, RedisCacheConfiguration> configMap = new HashMap<>();
//        configMap.put("router", cacheConfiguration.entryTtl(Duration.ofSeconds(120)));
//        configMap.put("pay_platform", cacheConfiguration.entryTtl(Duration.ofSeconds(300)));
        configMap.put("router", cacheConfiguration);
        configMap.put("pay_platform", cacheConfiguration);
        configMap.put("middle_pay",cacheConfiguration.entryTtl(Duration.ofSeconds(3600)));

        // 使用自定义的缓存配置初始化一个cacheManager
        // 注意这两句的调用顺序，一定要先调用该方法设置初始化的缓存名，再初始化相关的配置
        RedisCacheManager cacheManager = RedisCacheManager.builder(factory)
                .initialCacheNames(cacheNames)
                .withInitialCacheConfigurations(configMap)
                .build();
        return cacheManager;
    }

}
